/*
 * Copyright (c) 2011  Cristian Constantin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package com.quizio.engine.model;

import com.quizio.engine.enums.QuizSessionState;
import com.quizio.engine.model.context.Views;
import com.quizio.model.User;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonView;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @author Cristian Constantin
 * @since 10/16/11
 */
@Entity
@Table(name = "quiz_sessions")
public class QuizSession implements Serializable {

    private Long id;

    private User user;

    private Quiz quiz;

    private Date startTime;
    private Date endTime;

    /**
     * The 1-based index of the session's active question.
     * The default value of 0 means that the quiz is not yet started.
     */
    @JsonView(Views.Player.class)
    private Integer currentQuestion = 0;

    private Boolean active = Boolean.FALSE;

    private Boolean closed = Boolean.FALSE;

    private QuizSessionState state = QuizSessionState.AVAILABLE;

    /**
     * The score obtained by the user on a scale from 0 to 100.
     * It is computed automatically after the user submits the quiz.
     */
    private Double score;

    private List<QuestionUserInput> questionsInputs;

    public QuizSession() {
    }

    public QuizSession(User user, Quiz quiz) {
        this.user = user;
        this.quiz = quiz;
    }

    @Id
    @SequenceGenerator(name = "quiz_sessions_pk_sequence", sequenceName = "quiz_sessions_id_seq")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "quiz_sessions_pk_sequence")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "start_time")
    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    @Column(name = "end_time")
    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    @ManyToOne
    @JsonIgnore
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @ManyToOne
    public Quiz getQuiz() {
        return quiz;
    }

    public void setQuiz(Quiz quiz) {
        this.quiz = quiz;
    }

    @Column
    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    @Column
    public Boolean getClosed() {
        return closed;
    }

    public void setClosed(Boolean closed) {
        this.closed = closed;
    }

    @Column
    public QuizSessionState getState() {
        return state;
    }

    public void setState(QuizSessionState state) {
        this.state = state;
    }

    @Column
    public Double getScore() {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    @OneToMany(mappedBy = "quizSession")
    @Cascade(CascadeType.ALL)
    @OrderBy("id")
    @JsonView(Views.ReportingDetails.class)
    public List<QuestionUserInput> getQuestionsInputs() {
        return questionsInputs;
    }

    public void setQuestionsInputs(List<QuestionUserInput> questionsInputs) {
        this.questionsInputs = questionsInputs;
    }

    @Column(name = "current_question")
    public Integer getCurrentQuestion() {
        return currentQuestion;
    }

    public void setCurrentQuestion(Integer currentQuestion) {
        this.currentQuestion = currentQuestion;
    }
}
